<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>财务管理</el-breadcrumb-item>
            <el-breadcrumb-item>开票管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>

        <el-form class="demo-form-inline" :model="FinanceLoanDB" ref="queryform_FinanceInvoice">
            <el-row :gutter="5" class="row-bg">
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="BillNumber">
                        <el-input v-model="FinanceLoanDB.BillNumber" placeholder="请输入开票单号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="InvoiceType">
                        <el-select filterable style="width:100%" v-model="FinanceLoanDB.InvoiceType" required placeholder="请选择类型">
                            <el-option :value="0" label="请选择发票类型"></el-option>
                            <el-option :value="1" label="增值税专用发票"></el-option>
                            <el-option :value="2" label="增值税普通发票"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="DepartmentId">
                        <el-select filterable style="width:100%" v-model="FinanceLoanDB.DepartmentId" required placeholder="请选择部门">
                            <el-option :value="0" label="请选择部门"></el-option>
                            <el-option v-for="(v,k) in AllDepartment" :value="v.Id" :data-userid="v.Id" :label="v.DepartmentName" v-text="v.DepartmentName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="ProjectId">
                        <el-select filterable style="width:100%" v-model="FinanceLoanDB.ProjectId" required placeholder="请选择项目">
                            <el-option :value="0" label="请选择项目"></el-option>
                            <el-option v-for="(v,k) in AllProject" :value="v.Id" :data-userid="v.Id" :label="v.ProjectName" v-text="v.ProjectName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="ApplyUserId">
                        <el-select filterable style="width:100%" v-model="FinanceLoanDB.ApplyUserId" required placeholder="请选择申请人">
                            <el-option :value="0" label="请选择申请人"></el-option>
                            <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-date-picker v-model="form.Start"
                                    style="width:100%"
                                    type="date"
                                    placeholder="开始时间">
                    </el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-date-picker v-model="form.End"
                                    style="width:100%"
                                    type="date"
                                    placeholder="结束时间">
                    </el-date-picker>
                </el-col>

            </el-row>
            <el-row :gutter="5" class="row-bg">
                <el-col :span="1.5">
                    <el-checkbox style="width:100%" v-model="form.NoChecked" label="我未审核" border></el-checkbox>
                </el-col>
                <el-col :span="1.5">
                    <el-checkbox style="width:100%" v-model="FinanceLoanDB.IsRefuse" label="被拒绝单据" border></el-checkbox>
                </el-col>

                <el-col :span="1">
                    <el-button icon="el-icon-search" style="width:100%" @click="GetFinanceInvoiceDetail()"> </el-button>
                </el-col>

                <el-col :span="1">
                    <el-button icon="el-icon-refresh-right" style="width:100%" @click="GetFinanceInvoiceDetail_Refresh()"> </el-button>
                </el-col>

                <el-col :span="1.5">
                    <!--<el-button icon="el-icon-search" size="medium" style="width:100px" @click="_GetFinanceLoanDetail()">搜索</el-button>-->
                    <el-button style="width:100%"
                               icon="el-icon-circle-plus-outline" type="success" @click="ShowAddFinanceLoan">添加</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-row style="padding:10px 0px">
            <el-col>
                <el-table :data="tableData"
                          border>
                    <el-table-column header-align="center" fixed="left" type="selection" width="50" align="center"></el-table-column>
                    <el-table-column header-align="center" fixed="left" type="index" label="序号" align="center" :index="_indexMethod"></el-table-column>
                    <el-table-column header-align="center" fixed="left" align="center" prop="BillNumber" label="开票单号" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="TotalPrice" label="开票金额" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="CollectionAmount" label="收款金额" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="TotalPrice" label="作废金额">
                        <template slot-scope="scope">
                            <label style='color:#1E90FF'>{{scope.row.CancelMoney}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="RedMarkMoney" label="红字金额">
                        <template slot-scope="scope">
                            <label style='color: red'>{{scope.row.RedMarkMoney}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="Money" label="领票人" width="100px">
                        <template slot-scope="scope">
                            <label>{{_UserById(scope.row.InvoiceReceiver)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="InvoiceDate" label="开票日期" width="130px">
                        <template slot-scope="scope">
                            {{fmtDate(scope.row.InvoiceDate)}}
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="ReversalMoney" label="发票类型" width="130px">
                        <template slot-scope="scope">
                            <label v-if="scope.row.InvoiceType==1">增值税专用发票</label>
                            <label v-else-if="scope.row.InvoiceType==2">增值税普通发票</label>
                            <label v-else></label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="ApplyUserId" width="130px" label="申请人">
                        <template slot-scope="scope">
                            <label>{{_UserById(scope.row.ApplyUserId)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="ApplyDate" label="申请日期" width="130px">
                        <template slot-scope="scope">
                            {{fmtDate(scope.row.ApplyDate)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="State" label="状态" header-align="center" align="center">
                        <template slot-scope="scope">
                            <label v-html="InitStateColum(scope.row)"> </label>
                        </template>
                    </el-table-column>

                    <el-table-column header-align="center" align="center" prop="CollectionAmount" label="税率" width="150px">
                        <template slot-scope="scope">
                            <label>{{(scope.row.TaxRate)}}%</label>
                        </template>
                    </el-table-column>

                    <el-table-column header-align="center" align="center" prop="Content" label="开票内容" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Model" label="规格" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Unit" label="单位" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Amount" label="数量" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Price" label="单价" width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="InvoiceRemark" label="发票备注" width="150px" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Price" label="甲方单位" :show-overflow-tooltip="true" width="150px">
                        <template slot-scope="scope">
                            <label>{{_AllCustomerById(scope.row.CunstomerId)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="ContactUser" label="甲方联系人" width="150px"></el-table-column>

                    <el-table-column header-align="center" align="center" prop="DepartmentId" label="部门" width="150px">
                        <template slot-scope="scope">
                            <label>{{_GEtAllDepartmentById(scope.row.DepartmentId)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="Price" label="项目" width="150px" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <label>{{_ProjectById(scope.row.ProjectId)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="Remark" label="单据说明" width="150px" :show-overflow-tooltip="true"></el-table-column>

                    <el-table-column prop="address" header-align="center" align="center" label="操作" fixed="right" width="300px">
                        <template slot-scope="scope">
                            <el-button type="success" class="BtnSize" size="mini" plain @click="_order(scope.row)">开票单</el-button>
                            <el-button type="info" class="BtnSize" size="mini" plain @click="_editResult(scope.row)">编辑</el-button>
                            <el-button type="primary" class="BtnSize" size="mini" plain @click="_invoice(scope.row)">开票</el-button>
                            <el-button type="primary" class="BtnSize" size="mini" plain @click="_collection(scope.row,'单据收款','true')">收款</el-button>
                            <el-button type="warning" class="BtnSize" size="mini" plain @click="_reOpen(scope.row,'单据重开','false')">重开</el-button>
                            <el-button type="warning" class="BtnSize" size="mini" plain @click="_cancel(scope.row)">作废</el-button>
                            <el-button type="danger" class="BtnSize" size="mini" plain @click="_DelFinanceInvoice(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination @size-change="GetFinanceInvoiceDetail"
                                   @current-change="GetFinanceInvoiceDetail"
                                   :current-page.sync="pagination.currentPage"
                                   :page-sizes="pagination.pagesizes"
                                   :page-size.sync="pagination.pagesize"
                                   layout="total, sizes, prev, pager, next, jumper"
                                   :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>



        <el-row style="padding:10px 0px">
            <el-col>
                <el-dialog :visible.sync="detailCashierShow" width="40%" :title="detailCashierTitleName" @close="GetFinanceInvoiceDetail">
                    <el-form class="demo-form-inline" :model="detailCashier" label-width="40px">
                        <div>
                            <el-form-item label="金额">
                                <el-input type="text" v-model="detailCashier.money"></el-input>
                            </el-form-item>
                        </div>
                        <div style="text-align:center">
                            <el-button type="success" @click="OndetailCashier">确定</el-button>
                        </div>
                    </el-form>
                </el-dialog>

            </el-col>
        </el-row>

        <div>
            <el-dialog :visible.sync="AUFinanceInvoiceShow" width="60%"  top="10px"  :title="titlename">
                <AUFinanceInvoice :AUFinanceInvoice="AUFinanceInvoice"  :AllCustomer="AllCustomer" :AllUsers="AllUsers" :AllProject="AllProject" :AllDepartment="AllDepartment"></AUFinanceInvoice>
            </el-dialog>
        </div>

        <div>
            <el-dialog :visible.sync="AddFinanceInvoiceManageShow" width="60%" title="开具发票" @close="closeSave">
                <AddFinanceInvoiceManage :AllUsers="AllUsers" :BillNumber="BillNumber"></AddFinanceInvoiceManage>
            </el-dialog>
        </div>

        <div>
            <el-dialog :visible.sync="FinanceInvoiceOrderShow" width="80%" :fullscreen="true" :title="FinanceInvoiceTitle" @close="GetFinanceInvoiceDetail">
                <FinanceInvoiceOrder :AllUsers="AllUsers"
                                     :AllCustomer="AllCustomer"
                                     :AllProject="AllProject"
                                     :AllDepartment="AllDepartment"
                                     :IdNUmber="IdNUmber"
                                     :OrderTableAllDB="OrderTableAllDB"
                                     ></FinanceInvoiceOrder>
            </el-dialog>
        </div>

        <div>
            <el-dialog :visible.sync="UpFinanceInvoiceShow" width="60%"  top="10px"  title="编辑开票信息">
                <UpFinanceInvoice :UpFinanceInvoice="AUFinanceInvoice"   :AllCustomer="AllCustomer" :AllUsers="AllUsers" :AllProject="AllProject" :AllDepartment="AllDepartment"></UpFinanceInvoice>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import * as publicjs from "/public/Scriput/Public"
    import AUFinanceInvoice from "./AUFinanceInvoice"
    import UpFinanceInvoice from "./UpFinanceInvoice"
    import AddFinanceInvoiceManage from './AddFinanceInvoiceManage'
    import FinanceInvoiceOrder from './FinanceInvoiceOrder'
    export default {
        name: "FinanceInvoice",
        components: {
            'AUFinanceInvoice': AUFinanceInvoice,
            'AddFinanceInvoiceManage': AddFinanceInvoiceManage,
            'FinanceInvoiceOrder': FinanceInvoiceOrder,
            'UpFinanceInvoice': UpFinanceInvoice
        },
        data() {
            return {
                IdNUmber:0,
                titlename: "添加开票单",
                detailCashierShow: false,
                detailCashierTitleName: '',
                AUFinanceInvoiceShow: false,
                UpFinanceInvoiceShow: false,
                FinanceInvoiceOrderShow: false,
                AddFinanceInvoiceManageShow: false,
                BillNumber: '',
                FinanceInvoiceTitle: '',

                AllUsers: [],
                AllDepartment: [],
                AllCustomer: [],
                AllProject: [],
                tableData: [],
                GetPayArray: [],
                //删除Id
                delidarray: [],
                FinanceLoanDB: {
                    // LoanNumber: '',
                    // FinanceType: '',
                    // Project: '',
                    // Department: '',
                    // Borrower: '',
                    IsRefuse: false,
                },
                form: {
                    Start: '',
                    End: '',
                    // Payee: '',
                    NoChecked: false,
                    // StartMoney: '',
                    // EndMoney: '',
                },
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 0
                },
                detailCashier: {
                    Id: '',
                    money: '',
                    detabit: '',
                },
                OrderTableAllDB: {},
                AUFinanceInvoice: {
                    Borrower: '',
                    Departme: '',
                    Project: '',
                    FinanceType: '',
                    ContactUser: '',
                    Content: '',
                    Model: '',
                    Unit: '',
                    Amount: '',
                    Price: '',
                    TotalPrice: '',
                    TaxRate: '',
                    InvoiceRemark: '',
                    Remark: '',
                },
            }
        },
        created() {
            this.AllProject = publicjs.GetAllProject(this.$http);
            this.AllUsers = publicjs.GetCompanyAllUsers(this.$http);
            this.AllCustomer = publicjs.GetAllCustomer(this.$http);
            this.GetPayArray = publicjs.GetPayArray(this.$http);
            this.GetFinanceInvoiceDetail();
        },
        methods: {
            fmtDate(obj) {
                var objstr = obj.split(' ')[0];
                if (objstr != '0001-01-01') {
                    return objstr;
                } else {
                    return "";
                }
            },
            InitStateColum(d) {
                var result = "";
                result = publicjs.InitStateColum(d)
                if (d.State == 128) {
                    result = "<span style='color:#CD2626'>已开票</span>";
                }
                else if (d.State == 256) {
                    result = "<span style='color:#1c1f1f'>已作废</span>";
                }
                else if (d.State == 512) {
                    result = "<span style='color:#1c1f1f'>已红字</span>";
                }
                else if (d.State == 1024) {
                    result = "<span style='color:#CD2626'>已重开</span>";
                }
                return result;
            },
            ShowAddFinanceLoan() {
                this.AUFinanceInvoice = {};
                this.titlename = "添加开票单";
                this.AUFinanceInvoiceShow = true;
            },
            _UserById(id) {
                for (var i = 0; i < this.AllUsers.length; i++) {
                    if (this.AllUsers[i].Id == id) {
                        return this.AllUsers[i].UserName;
                    }
                }
            },
            _AllCustomerById(PayeeId) {
                for (var i = 0; i < this.AllCustomer.length; i++) {
                    if (this.AllCustomer[i].Id == PayeeId) {
                        return this.AllCustomer[i].CustomerCompany;
                    }
                }
            },
            //根据ID获取部门Name
            _GEtAllDepartmentById(scope) {
                for (var i = 0; i < this.AllDepartment.length; i++) {
                    if (this.AllDepartment[i].Id == scope) {
                        return this.AllDepartment[i].DepartmentName;
                    }

                }
            },
            //根据ID获取项目Name
            _ProjectById(d) {
                for (var i = 0; i < this.AllProject.length; i++) {
                    if (this.AllProject[i].Id == d) {
                        return this.AllProject[i].ProjectName;
                    }
                }
            },

            _indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },

            //初始化表格数据加载
            GetFinanceInvoiceDetail() {
                //(int limit, int offset, FinanceLoan finance, DateTime? Start, DateTime? End, bool NoChecked, string StartMoney, string EndMoney)
                let loadding = this.openLoading();
                this.$http.post("/Finance/GetFinanceInvoiceDetail", {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    form: this.form,
                    finance: this.FinanceLoanDB,
                }).then(x => {
                    loadding.close();
                    this.pagination.total = x.data.count
                    this.tableData = x.data.data;
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //重置查询
            GetFinanceInvoiceDetail_Refresh(){
                this.$refs["queryform_FinanceInvoice"].resetFields()
                this.form.NoChecked = false
                this.form.Start = ''
                this.form.End = ''
                this.FinanceLoanDB.IsRefuse = false
                this.GetFinanceInvoiceDetail()
            },

            _order(obj) {
                this.IdNUmber = obj.Id;
                this.$http.get('/Finance/FinanceInvoiceOrder?Id=' + obj.Id).then((result) => {
                    if (result.data.mes.IsSuccess) {
                        this.OrderTableAllDB = result.data;
                        this.FinanceInvoiceTitle = "开票单---" + obj.BillNumber;
                        this.FinanceInvoiceOrderShow = true;
                    } else {
                        this.$message.error(result.data.mes.Msg)
                    }
                });
            },
            //编辑
            _editResult(obj) {

                this.$http.post('/Finance/ValidateFinanceInvoiceState', { dataId: obj.Id }).then((result) => {
                    var message = result.data;
                    if (message.IsSuccess == false) {
                        this.$message(message.Msg);
                    } else {
                        console.log(obj);
                        this.AUFinanceInvoice = obj;
                        this.UpFinanceInvoiceShow = true;
                    }
                })
                // this.ShowUpdate(obj.Id);
            },
            //删除相关数据信息
            _DelFinanceInvoice(row) {
                this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    var arrid = [];
                    arrid.push(row.Id);
                    let loadding = this.openLoading();
                    this.$http.post("/Finance/DeleteFinanceInvoice", arrid).then(x => {
                        loadding.close();
                        console.log(x.data)
                        if (x.data.IsSuccess) {
                            this.$message.success(x.data.Msg)
                            this.GetFinanceInvoiceDetail();
                        } else {
                            this.$message.error(x.data.Msg)
                        }
                    })
                });
            },

            //开票
            _invoice(obj) {
                this.$http.get('/Finance/AddFinanceInvoiceManageBit?BillNumber=' + obj.BillNumber).then(result => {
                    var message = result.data;
                    console.log(message)
                    if (message.IsSuccess) {
                        this.BillNumber = obj.BillNumber;
                        this.AddFinanceInvoiceManageShow = true;
                    } else {
                        this.$message.error(message.Msg);
                    }
                });
            },

            //收款
            _collection(obj, str, bit) {
                this.detailCashier.detabit = bit;
                this.detailCashier.Id = obj.Id;
                this.detailCashierTitleName = str;
                this.detailCashierShow = true;
            },
            //重开
            _reOpen(obj, str, bit) {
                this.detailCashier.detabit = bit;
                this.detailCashier.Id = obj.Id;
                this.detailCashierTitleName = str;
                this.detailCashierShow = true;
            },


            OndetailCashier() {
                if (this.detailCashier.detabit != 'true') {
                    var str = '/Finance/FinanceInvoiceReOpen';
                    //财务重新进行开票申请操作
                    this.CashierReOpen(str);
                } else {
                    var str = '/Finance/FinanceInvoiceCollection';
                    //财务收款操作
                    this.CashierReOpen(str);
                }

                this.detailCashier.money = '';
                this.detailCashierShow = false;
            },

            //作废
            _cancel(obj) {
                let _this = this;
                this.$confirm("此操作将该数据作废, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then((res) => {
                    let loadding = this.openLoading();
                    _this.$http.post('/Finance/CancelFinanceInvoiceManageByBillNumber', { BillNumber: obj.BillNumber }).then((result) => {
                        loadding.close();
                        if (result.data.IsSuccess) {
                            _this.$message.success(result.data.Msg)
                            this.GetFinanceInvoiceDetail()
                        } else {
                            _this.$message.error(result.data.Msg)
                        }
                    });

                })
            },
            //财务重新进行开票申请操作
            CashierReOpen(str) {
                var _this = this;
                _this.$http.post(str, { Id: _this.detailCashier.Id, money: _this.detailCashier.money }).then((result) => {
                    console.log(result);
                    if (result.data.IsSuccess) {
                        _this.$message.success(result.data.Msg)

                    } else {
                        _this.$message.error(result.data.Msg)
                    }
                });
            },

            closeSave() {
                this.GetFinanceInvoiceDetail();
            },



        },




    }
</script>
<style scoped>
    .big_box {
        padding: 15px;
    }

    .row-bg {
        padding: 10px 0;
        background-color: #f9fafc;
    }

    .BtnSize {
        margin-top: 8px;
    }
    .elformitem{
        margin-bottom: 0px !important;
    }
</style>
